<template>
  <view>
    <u-navbar title="子账号管理" title-size="38" :border-bottom="false" />

    <view class="phone-box" v-for="(item, index) in phones">
      <view class="phone-box-title"> 手机号： </view>
      <input
        class="phone-box-input"
        v-model="phones[index].phone"
        type="number"
        placeholder="输入子账号手机号"
        :maxlength="11"
        @input="inputPhone"
        @focus="focusPhone(index)"
        :disabled="phones[index].phone.length == 11"
      />
      <image
        class="phone-box-add"
        @click="addPhone"
        src="../../static/img/car-plus.svg"
        v-if="index == phones.length - 1"
      ></image>
      <image
        class="phone-box-close"
        @click="delPhone(phones[index].sub_id)"
        src="../../static/img/close.svg"
        v-else="index == phones.length - 1"
      ></image>
    </view>

    <view class="car-btn btn">确定</view>

    <u-toast ref="uToast" />
    <u-modal
      v-model="isModel"
      :content="modelText"
      :show-cancel-button="true"
      @confirm="carDelete()"
    ></u-modal>

    <u-modal
      v-model="isPhoneModel"
      :content="modelPhoneText"
      :show-cancel-button="true"
      @confirm="phoneDelete()"
    ></u-modal>
  </view>
</template>

<script>
import { deleteSubuser, createSubuser, getSubuser } from "@/api/subuser.js";

export default {
  data() {
    return {
      isCar: false,
      carCard: "",
      isModel: false,
      isPhoneModel: false,
      phoneIndex: 0,
      subId: "",
      inputIndex: 0,
      modelText: "确定要删除车辆吗?",
      modelPhoneText: "确定要删除手机号吗?",
      phones: [],
    };
  },
  created() {
    getSubuser().then((res) => {
      if (res.data.code == 200) {
        this.phones = res.data.data.data;
        if (this.phones.length == 0) {
          this.phones.push({ phone: "" });
        }
      }
    });
  },
  methods: {
    // 添加或修改手机号
    inputPhone(val) {
      if (val.detail.value.length == 11) {
        if (
          !val.detail.value.match(
            /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
          )
        ) {
          this.$refs.uToast.show({
            title: "请输入正确的手机号!",
            type: "error",
            position: "bottom",
          });
          this.phones[this.inputIndex].phone = "";
          return;
        }
        createSubuser({
          phone: val.detail.value,
        }).then((res) => {
          if (res.data.code == 200) {
            this.$refs.uToast.show({
              title: "添加成功!",
              type: "success",
              position: "bottom",
            });
            this.phones[this.inputIndex].sub_id = res.data.data.sub_id;
          } else {
            this.$refs.uToast.show({
              title: res.data.msg,
              type: "error",
              position: "bottom",
            });
            this.phones[this.inputIndex].phone = "";
            return;
          }
        });
      }
    },
    focusPhone(index) {
      this.inputIndex = index;
    },
    addPhone() {
      this.phones.push({ phone: "" });
    },
    checkNew() {
      // this.carItem.is_new_energy = this.carItem.is_new_energy == 1 ? 0 : 1;
    },

    delPhone(id) {
      this.isPhoneModel = true;
      this.subId = id;
    },
    // 删除手机号
    phoneDelete() {
      // 在this.phones中删除与sub_id相同的数据
      this.phones = this.phones.filter((item) => {
        return item.sub_id != this.subId;
      });
      deleteSubuser(this.subId).then((res) => {
        if (res.data.code == 200) {
          // 删除phones数组中的对应的数据
          this.$refs.uToast.show({
            type: "success",
            title: "删除成功",
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  font-size: 66rpx;
  font-weight: bold;
  text-align: center;
  margin-top: 60rpx;
}

.content {
  width: 420rpx;
  margin: 20rpx auto;
  text-align: center;
  font-size: 28rpx;
  color: #666666;
}

.car-input {
  border: 4rpx solid #1a88ff;
  margin: 60rpx;
  padding: 30rpx 20rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;

  &-item {
    width: 60rpx;
    height: 78rpx;
    position: relative;
    z-index: 9999999;
    margin: 6rpx;
    font-size: 30rpx;
    font-weight: bold;
    color: #303133;
    line-height: 45px;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -webkit-flex-direction: row;
    flex-direction: row;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    // flex-direction: column;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    border: 1px solid #606266;
    border-radius: 6rpx;
  }
}

.checkNew {
  background-color: #1a88ff;
  color: #ffffff;
}

.new {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  .plus {
    width: 24rpx;
    height: 24rpx;
    margin-bottom: 14rpx;
  }

  .name {
    font-size: 20rpx;
    position: absolute;
    width: 100rpx;
    text-align: center;
    bottom: -26rpx;
    transform: scale(0.7);
    color: #b8b8b8;
  }
}

.phone-box {
  margin: 20rpx 60rpx 0;
  border-radius: 16rpx;
  border: 4rpx solid #1a88ff;
  display: flex;
  align-items: center;
  padding: 30rpx 20rpx;
  justify-content: space-between;

  &-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333333;
  }

  &-input {
    flex: 1;
    font-size: 32rpx;
    color: #333333;
  }

  &-add {
    width: 42rpx;
    height: 42rpx;
  }

  &-close {
    width: 42rpx;
    height: 42rpx;
    transform: scale(1.3);
  }
}

.car-btn {
  width: 480rpx;
  height: 88rpx;
  background: linear-gradient(90deg, #1a88ff 0%, #2c6bfc 100%);
  box-shadow: 0px 6px 18px rgba(26, 136, 255, 0.3);
  border-radius: 42px;
  color: #ffffff;
  font-size: 36rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 120rpx auto 0;
}

.car-false {
  width: 480rpx;
  color: #666666;
  font-size: 36rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 40rpx auto 0;
}
</style>
